/* filesys.c : dgnsc file system func
 * Copyright (C) 2009-2010 drangon <drangon.zhou@gmail.com>
 * 2011-02
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "filesys.h"
#include <dgnsc/common.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/file.h>  // flock
#endif

struct dgn_file_st
{
#ifdef _WIN32
	HANDLE fd;
#else
	int fd;
#endif
};

dgn_file_t dgn_file_init( const char * name, int create_flag )
{
#ifdef _WIN32
	DWORD faccess, fcreate;
	if( create_flag == DGN_O_OPEN_CREATE_EXCL ) {
		faccess = GENERIC_READ | GENERIC_WRITE;
		fcreate = CREATE_NEW;
	}
	else if( create_flag == DGN_O_OPEN_CREATE ) {
		faccess = GENERIC_READ | GENERIC_WRITE;
		fcreate = OPEN_ALWAYS;
	}
	else if( create_flag == DGN_O_OPEN_WRITE ) {
		faccess = GENERIC_READ | GENERIC_WRITE;
		fcreate = OPEN_EXISTING;
	}
	else {
		faccess = GENERIC_READ;
		fcreate = OPEN_EXISTING;
	}

	HANDLE fd = CreateFile( name, faccess, 0, NULL, fcreate, FILE_ATTRIBUTE_NORMAL, NULL );
	if( fd == INVALID_HANDLE_VALUE )
		return NULL;
#else
	int flag;
	if( create_flag == DGN_O_OPEN_CREATE_EXCL )
		flag = O_RDWR | O_CREAT | O_EXCL;
	else if( create_flag == DGN_O_OPEN_CREATE )
		flag = O_RDWR | O_CREAT;
	else if( create_flag == DGN_O_OPEN_WRITE )
		flag = O_RDWR;
	else
		flag = O_RDONLY;

	int fd = open( name, flag, 0660 );
	if( fd < 0 )
		return NULL;
#endif
	struct dgn_file_st * ft = (struct dgn_file_st *)malloc( sizeof(struct dgn_file_st) );
	ft->fd = fd;
	return ft;
}

int64_t dgn_file_seek( dgn_file_t ft, int64_t off, int where )
{
	if( ft == NULL ) {
		PR_DEBUG( "ft is NULL" );
		return 0;
	}
#ifdef _WIN32
	int mywhere = FILE_BEGIN;
	if( where == DGN_SEEK_SET )
		mywhere = FILE_BEGIN;
	else if( where == DGN_SEEK_CUR )
		mywhere = FILE_CURRENT;
	else if( where == DGN_SEEK_END )
		mywhere = FILE_END;
	LARGE_INTEGER liin, liout;
	liin.QuadPart = off;
	liout.QuadPart = -1;
	SetFilePointerEx( ft->fd, liin, &liout, mywhere );
	return liout.QuadPart;
#else
	int mywhere = SEEK_SET;
	if( where == DGN_SEEK_SET )
		mywhere = SEEK_SET;
	else if( where == DGN_SEEK_CUR )
		mywhere = SEEK_CUR;
	else if( where == DGN_SEEK_END )
		mywhere = SEEK_END;
	return lseek( ft->fd, off, mywhere );
#endif
}

int dgn_file_read( dgn_file_t ft, char * buf, int len )
{
	if( ft == NULL ) {
		PR_DEBUG( "ft is NULL" );
		return -1;
	}
#ifdef _WIN32
	DWORD retlen = -1;
	ReadFile( ft->fd, buf, len, &retlen, NULL );
	return retlen;
#else
	return read( ft->fd, buf, len );
#endif
}

int dgn_file_write( dgn_file_t ft, const char * buf, int len )
{
	if( ft == NULL ) {
		PR_DEBUG( "ft is NULL" );
		return -1;
	}
#ifdef _WIN32
	DWORD retlen = -1;
	if( WriteFile( ft->fd, buf, len, &retlen, NULL ) == 0 )
		return -1;
	return retlen;
#else
	return write( ft->fd, buf, len );
#endif
}

int dgn_file_lock( dgn_file_t ft )
{
	if( ft == NULL ) {
		PR_DEBUG( "ft is NULL" );
		return -1;
	}
#ifdef _WIN32
	OVERLAPPED ol;
	memset( &ol, 0, sizeof(ol) );
	BOOL ret = LockFileEx( ft->fd, LOCKFILE_EXCLUSIVE_LOCK, 0, 0x7FFFFFFF, 0, &ol );
	return ( ret ) ? 0 : -1;
#else
	return flock( ft->fd, LOCK_EX );
#endif
}

int dgn_file_unlock( dgn_file_t ft )
{
	if( ft == NULL ) {
		PR_DEBUG( "ft is NULL" );
		return -1;
	}
#ifdef _WIN32
	OVERLAPPED ol;
	memset( &ol, 0, sizeof(ol) );
	BOOL ret = UnlockFileEx( ft->fd, 0, 0x7FFFFFFF, 0, &ol );
	return ( ret ) ? 0 : -1;
#else
	return flock( ft->fd, LOCK_UN );
#endif
}

int dgn_file_truncate( dgn_file_t ft, int64_t off )
{
	if( ft == NULL ) {
		PR_DEBUG( "ft is NULL" );
		return -1;
	}
#ifdef _WIN32
	dgn_file_seek( ft, off, DGN_SEEK_SET );
	SetEndOfFile( ft->fd );
#else
	if( ftruncate( ft->fd, off ) < 0 )
		return -1;
	dgn_file_seek( ft, off, DGN_SEEK_SET );
#endif
	return 0;
}

void dgn_file_fini( dgn_file_t ft )
{
	if( ft == NULL )
		return;
#ifdef _WIN32
	CloseHandle( ft->fd );
#else
	close( ft->fd );
#endif
	free( ft );
	return;
}

int dgn_file_rm( const char * name )
{
#ifdef _WIN32
	if( DeleteFile( name ) )
		return 0;
	return -1;
#else
	return unlink( name );
#endif
}

